package com.tedu.controller;

import com.tedu.entity.Dept;
import com.tedu.service.impl.DeptServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DeptServlet", value = "/dept/all")
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置响应字符编码
        response.setCharacterEncoding("UTF-8");

        // 获取请求参数

        // 调用业务逻辑
        DeptServiceImpl deptService = new DeptServiceImpl();
        List<Dept> allDept = deptService.findAllDept();
        // 将结合元素转化为json字符
        JSONArray jsonArray = JSONArray.fromObject(allDept);
        //将相应的json串打印的前端
        response.getWriter().write(jsonArray.toString());

    }
}
